package com.handpay.zztong.hp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.HttpEngine;
import com.handpay.framework.LuaTableUtil;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.config.ZZTConfig;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.main.model.FunctionInfo;
import com.handpay.zztong.hp.protocol.ATProtocolConstant;
import com.handpay.zztong.hp.protocol.BaseProtocolKey;
import com.handpay.zztong.hp.ui.DialogFactory;
import com.handpay.zztong.hp.utils.AccountUtils;
import com.handpay.zztong.hp.utils.AccountUtils.AccountStatus;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import se.krka.kahlua.vm.LuaTable;

public class FinancingAreaActivity extends ZZTongActivity {

	private GridView gv;
	protected int newWidth = 80;
	private List<FunctionInfo> data ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.financ_index);
		super.onCreate(savedInstanceState);
		newWidth = this.getResources().getDisplayMetrics().widthPixels;
		initDatas();
		initView();
		
	}

	@Override
	protected boolean isShownTabs() {
		return false;
	}
	@Override
	public ActionBar getActivityActionBar() {
		return new ActionBar("微理财", true);
	}
	private void initDatas() {
		data = new ArrayList<FunctionInfo>();
		FunctionInfo info = new FunctionInfo("gfhcq", "广发“慧存钱”", new int[]{R.drawable.icon_gflc}, GfFoundIndex.class);
		data.add(info);
		
	}

	private void initView() {
		gv = (GridView) findViewById(R.id.finaGV);
		gv.setAdapter(new MyBaseAdapter(this));
		gv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(position < 0){
					return;
				}
				FunctionInfo info = data.get(position);
				if("gfhcq".equals(info.itemId)){
					goToGFActivity();
				}
			}
		});
	}
	
	@Override
	public boolean netResponse(String action, Hashtable<String, Object> resp,
			boolean showAlert) {
		if(super.netResponse(action, resp, showAlert)){
			return true;
		}
		if(ZZTongActivity.Do_ZztGFFundIndex.equals(action)){
			 byte data[] = (byte[]) resp.get(HttpEngine.Key_RespData);
	 		 LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, isDataCompile);
	 		String responseCode = (String) luaTable.rawget("responseCode");
	 		String errorMessage = (String) luaTable.rawget("errMessage");
	     	LuaTable chartsTable = (LuaTable) luaTable.rawget("charts");
	 		if(errorMessage == null && responseCode ==null && chartsTable !=null ){
	 		Intent intent = new Intent(FinancingAreaActivity.this, GfFoundIndex.class);
     		intent.putExtra("indexData",data);
     		startActivity(intent);
//     		goBack();
	 		}else {
     		DialogFactory.showAlertDialog(FinancingAreaActivity.this, getString(R.string.tip),
                     errorMessage != null?errorMessage:getString(R.string.guangfa_system_error), true);
			}
		}
		return false;
	}
	
	protected void goToGFActivity() {
		if(AccountUtils.getAccountStatus(this).equals(AccountStatus.SUCCESS)){
    		HPLog.e("fyang", "跳转广发");
    		Hashtable<String, String> params  = new Hashtable<String, String>();
    	    params.put(BaseProtocolKey.CHANNEL, ClientEngine.CHANNEL);
    	    params.put(ATProtocolConstant.CLIENTCHANNEL, ZZTConfig.UPDATE_CHANNEL);
    		params.put(BaseProtocolKey.PRODUCTCODE, "HLC");
    		doPost(this, ZZTongActivity.Do_ZztGFFundIndex, params, true);
    	}else {
    		DialogFactory.showAlertDialog(this, getString(R.string.tip),
                    getString(R.string.toast_need_upload), true);
		}
	}
	public class MyBaseAdapter extends BaseAdapter {
		LayoutInflater inflater;
		Context myContext;

		public MyBaseAdapter(Context context) {
			super();
			myContext = context;
			inflater = LayoutInflater.from(context);

		}

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolder holder;
			if (position == 0 || convertView == null) {
				// 使用自定义的list_items作为Layout
				convertView = inflater.inflate(R.layout.fragment_home_item, null);
				convertView.setLayoutParams(new GridView.LayoutParams(
						(newWidth / 3) - 2, newWidth / 3));
				// 使用减少findView的次数
				holder = new ViewHolder();
				holder.imageView = (ImageView) convertView
						.findViewById(R.id.home_item_img);
				holder.itemsText = (TextView) convertView
						.findViewById(R.id.home_item_txt);
				holder.roundView = convertView.findViewById(R.id.notice_round);
				// 设置标记
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			FunctionInfo info = data.get(position);
			if (holder.roundView != null) {
				holder.roundView.setVisibility(View.GONE);
			}
			holder.imageView.setImageResource(info.iconResId[0]);
			holder.itemsText.setText(info.title);
			return convertView;
		}
	}
	
	/**
	 * ViewHolder类
	 */
	static class ViewHolder {
		ImageView imageView;
		TextView itemsText;
		View	 roundView;
	}
}
